package cn.ycl.system.biz.shop.controller;

import cn.ycl.system.biz.product.service.ProductService;
import cn.ycl.system.biz.shop.entity.KillInfo;
import cn.ycl.system.biz.shop.entity.vo.KillInfoVO;
import cn.ycl.system.biz.shop.service.KillInfoService;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * (KillInfo)表控制层
 *
 * @author yuanchangliang
 * @since 2021-09-30 16:11:17
 */
@RestController
@RequestMapping("/killInfo")
public class KillInfoController {
    /**
     * 服务对象
     */
    @Resource
    private KillInfoService killInfoService;
    @Resource
    private ProductService productService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("/selectOne")
    public KillInfo selectOne(Long id) {
        return this.killInfoService.queryById(id);
    }

    @GetMapping("/selectAll")
    public List<KillInfoVO> selectAll() {
        List<KillInfo> killInfos = killInfoService.queryAllByLimit(0, 3);
        List<KillInfoVO> infos = new ArrayList<>();
        killInfos.forEach(killInfo -> {
            KillInfoVO killInfoVO = new KillInfoVO();
            BeanUtils.copyProperties(killInfo, killInfoVO);
            killInfoVO.setProduct(productService.queryById(killInfo.getProductId()));
            killInfoVO.setBeginKillTime(getDiffTime(killInfo.getKillStartTime()));
            killInfoVO.setEndKillTime(getDiffTime(killInfo.getKillEndTime()));
            infos.add(killInfoVO);
        });
        return infos;
    }

    /**
     * 当前时间与传入时间的差值
     *
     * @param time
     * @return
     */
    public long getDiffTime(Date time) {
        long time1 = time.getTime();
        Date date = new Date();
        long time2 = date.getTime();
        return time2 - time1;
    }

}
